package com.example.li.day9_14;

import android.support.v4.os.CancellationSignal;
import android.support.v4.hardware.fingerprint.FingerprintManagerCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private TextView tv_msg;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_msg = findViewById(R.id.tv_msg);
    }

    private boolean canUseFingerprint(FingerprintManagerCompat compat) {
        if (!compat.isHardwareDetected()) {
            return false;
        }
        return compat.hasEnrolledFingerprints();
    }

    public void test(View view) {
        //获取指纹管理对象
        FingerprintManagerCompat fingerprintManagerCompat = FingerprintManagerCompat.from(this);
//        //手机是否有指纹识别模块
//        boolean hardwareDetected = fingerprintManagerCompat.isHardwareDetected();
//            //是否已经录入指纹
//            boolean b = fingerprintManagerCompat.hasEnrolledFingerprints();
            if (!canUseFingerprint(fingerprintManagerCompat)) {
                Toast.makeText(this,"没有指纹识别",Toast.LENGTH_SHORT).show();
        }
        tv_msg.setText("请进行指纹识别");
         cancellationSignal = new CancellationSignal();
        //开始验证指纹识别
        fingerprintManagerCompat.authenticate(null, 0, cancellationSignal, new FingerprintCallbask(), null);
    }
    CancellationSignal cancellationSignal;
    public void cancel(View view) {
        if (!cancellationSignal.isCanceled()) {
            cancellationSignal.cancel();
        }
    }

    private class FingerprintCallbask extends FingerprintManagerCompat.AuthenticationCallback{
        private static final String TAG = "MainActivity";

        /**
         * 当多次验证失败后,该方法被调用,并且在将来的一段事件不能在使用指纹验证.
         * @param errMsgId
         * @param errString
         */
        @Override
        public void onAuthenticationError(int errMsgId, CharSequence errString) {
            super.onAuthenticationError(errMsgId, errString);
            Log.d(TAG, "onAuthenticationError: "+errMsgId+","+errString);
            tv_msg.setText(errMsgId+""+errString);
        }

        /**
         * 在验证过程中,遇到可恢复的异常时该方法被调用(传感器脏了)
         * @param helpMsgId
         * @param helpString
         */
        @Override
        public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
            super.onAuthenticationHelp(helpMsgId, helpString);
            Log.d(TAG, "onAuthenticationHelp: "+helpString);
            tv_msg.setText(""+helpString);
        }

        /**
         * 指纹识别成功
         * @param result
         */
        @Override
        public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {
            super.onAuthenticationSucceeded(result);
            tv_msg.setText("指纹识别成功");
        }

        /**
         * 指纹识别失败
         */
        @Override
        public void onAuthenticationFailed() {
            super.onAuthenticationFailed();
            tv_msg.setText("指纹识别失败");
        }
    }
}
